﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MotionControl;
using WHW_NetUtility;
using System.Diagnostics;
using System.Threading;
using System.Windows.Forms;
using System.IO;
using HalconDotNet;
using Newtonsoft.Json;
using System.Text.RegularExpressions;
using XXTUserUI;

namespace MainFrame
{
    public partial class GlobalMotion
    {
        /// <summary>
        /// 单步-输送 入口治具顶起流程
        /// </summary>
        /// <param name="e_Step"></param>
        /// <returns></returns>
        public static int Step_ConveryUp(e_Step_ConveryUp e_Step, e_MachineStatus e_Staus = e_MachineStatus.暂停控制)
        {
            try
            {
                displalyInfo.displayInfoMain(e_Step.ToString());
                switch (e_Staus)
                {
                    case e_MachineStatus.无控制:
                        break;
                    case e_MachineStatus.暂停控制:
                        {
                            var nstop = MachineState(e_MachineStatus.暂停控制);
                            if (nstop != 1)
                                return -1;
                        }
                        break;
                }
                switch (e_Step)
                {
                    case e_Step_ConveryUp.上层输送治具:
                        return GlobalMotion.ConveryControrl(e_ConveryControl.上层输送启动, e_Staus);
                        break;
                    case e_Step_ConveryUp.入料段治具阻挡:
                        {
                            var nres = SetAir2(e_output_extCard2.上层阻挡气缸1, true, e_input_extCard1.上层阻挡气缸到位1, true);
                            if (nres != 1)
                            {//气缸未到位 多次顶起
                                for (int t = 0; t < GlobalVar.userConfig.airRepeatTime; t++)
                                {
                                    var res = GlobalMotion.MachineState(e_MachineStatus.暂停控制);
                                    if (res == -1)//触发了急停
                                        return -1;
                                    Thread.Sleep(GlobalVar.userConfig.airInterval);
                                    SetAir(e_output_extCard2.上层阻挡气缸1, false, e_input_extCard2.上层阻挡气缸原点1, true);
                                    nres = SetAir2(e_output_extCard2.上层阻挡气缸1, true, e_input_extCard1.上层阻挡气缸到位1, true);

                                    if (nres == 1)
                                        break;
                                }
                            }
                            return nres;
                        }
                        break;
                    case e_Step_ConveryUp.入料段治具放行:
                        {
                            var nres = SetAir(e_output_extCard2.上层阻挡气缸1, false, e_input_extCard2.上层阻挡气缸原点1, true);
                            if (nres != 1)
                            {
                                for (int t = 0; t < GlobalVar.userConfig.airRepeatTime; t++)
                                {
                                    var res = GlobalMotion.MachineState(e_MachineStatus.暂停控制);
                                    if (res == -1)//触发了急停
                                        return -1;
                                    Thread.Sleep(GlobalVar.userConfig.airInterval);
                                    SetAir2(e_output_extCard2.上层阻挡气缸1, true, e_input_extCard1.上层阻挡气缸到位1, true);
                                    nres = SetAir(e_output_extCard2.上层阻挡气缸1, false, e_input_extCard2.上层阻挡气缸原点1, true);

                                    if (nres == 1)
                                        break;
                                }
                            }
                            return nres;
                        }
                        break;
                    case e_Step_ConveryUp.二段治具下降:
                        {
                            var nres = SetAir(e_output_extCard2.上层顶升气缸2, false, e_input_extCard2.上层顶升气缸原点2, true);
                            if (nres != 1)
                            {
                                for (int t = 0; t < GlobalVar.userConfig.airRepeatTime; t++)
                                {
                                    var res = GlobalMotion.MachineState(e_MachineStatus.暂停控制);
                                    if (res == -1)//触发了急停
                                        return -1;
                                    Thread.Sleep(GlobalVar.userConfig.airInterval);
                                    SetAir2(e_output_extCard2.上层顶升气缸2, true, e_input_extCard1.上层顶升气缸到位2, true);
                                    nres = SetAir(e_output_extCard2.上层顶升气缸2, false, e_input_extCard2.上层顶升气缸原点2, true);
                                    if (nres == 1)
                                        break;
                                }
                            }
                            if (nres != 1 && e_Staus != e_MachineStatus.无控制)
                            {
                                for (int t = 0; t < 2; t++)
                                {
                                    //GlobalVar.isPause = true;
                                    if (1 == GlobalMotion.TipInfo($"{e_Step}失败,是否重试?  是:重试   否急停"))
                                    {
                                        t = 0;
                                        SetAir2(e_output_extCard2.上层顶升气缸2, true, e_input_extCard1.上层顶升气缸到位2, true);
                                        nres = SetAir(e_output_extCard2.上层顶升气缸2, false, e_input_extCard2.上层顶升气缸原点2, true);
                                        if (nres == 1)
                                            break;
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                                GlobalVar.isPause = false;
                            }
                            return nres;
                        }
                        break;
                    case e_Step_ConveryUp.二段治具升起:
                        {
                            var nres = SetAir2(e_output_extCard2.上层顶升气缸2, true, e_input_extCard1.上层顶升气缸到位2, true);
                            if (nres != 1)
                            {
                                Thread.Sleep(10);
                                SetAir2(e_output_extCard2.上层顶升气缸2, false, e_input_extCard1.上层顶升气缸到位2, true);
                                nres = SetAir2(e_output_extCard2.上层顶升气缸2, true, e_input_extCard1.上层顶升气缸到位2, true);
                                //GlobalVar.isPause = true;
                                if (1 == GlobalMotion.TipInfo($"二段治具升起不到位，是否重试?"))
                                {
                                    SetAir(e_output_extCard2.上层顶升气缸2, false, e_input_extCard2.上层顶升气缸原点2, true);
                                    nres = SetAir2(e_output_extCard2.上层顶升气缸2, true, e_input_extCard1.上层顶升气缸到位2, true);
                                    for (int t = 0; t < GlobalVar.userConfig.airRepeatTime; t++)
                                    {
                                        var res = GlobalMotion.MachineState(e_MachineStatus.暂停控制);
                                        if (res == -1)//触发了急停
                                            return -1;

                                        SetAir(e_output_extCard2.上层顶升气缸2, false, e_input_extCard2.上层顶升气缸原点2, true);
                                        Thread.Sleep(GlobalVar.userConfig.airInterval);
                                        nres = SetAir2(e_output_extCard2.上层顶升气缸2, true, e_input_extCard1.上层顶升气缸到位2, true);

                                        if (nres == 1)
                                            break;
                                    }
                                }
                                GlobalVar.isPause = false;
                            }

                            if (nres != 1 && e_Staus != e_MachineStatus.无控制)
                            {
                                for (int t = 0; t < 1; t++)
                                {
                                    //GlobalVar.isPause = true;
                                    if (1 == GlobalMotion.TipInfo($"{e_Step}失败,是否再重试?  是:重试   否急停"))
                                    {
                                        t = 0;
                                        SetAir(e_output_extCard2.上层顶升气缸2, false, e_input_extCard2.上层顶升气缸原点2, true);
                                        nres = SetAir2(e_output_extCard2.上层顶升气缸2, true, e_input_extCard1.上层顶升气缸到位2, true);
                                        if (nres == 1)
                                            break;
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                                GlobalVar.isPause = false;
                            }
                            return nres;
                        }
                        break;
                    case e_Step_ConveryUp.二段治具放行:
                        {
                            if (Input_CardEx2[(int)e_input_extCard2.上输送治具检测3] && Input_CardEx2[(int)e_input_extCard2.上输送治具检测4])
                            {
                                GlobalMotion.TipInfo($"出口信号检测超时，请检查是否堆料?");
                            }

                            var nres = SetAir(e_output_extCard2.上层阻挡气缸3, false, e_input_extCard2.上层阻挡气缸原点3, true);
                            if (nres != 1)
                            {
                                for (int t = 0; t < GlobalVar.userConfig.airRepeatTime; t++)
                                {
                                    var res = GlobalMotion.MachineState(e_MachineStatus.暂停控制);
                                    if (res == -1)//触发了急停
                                        return -1;
                                    Thread.Sleep(GlobalVar.userConfig.airInterval);
                                    SetAir2(e_output_extCard2.上层阻挡气缸3, true, e_input_extCard1.上层阻挡气缸到位3, true);
                                    nres = SetAir(e_output_extCard2.上层阻挡气缸3, false, e_input_extCard2.上层阻挡气缸原点3, true);

                                    if (nres == 1)
                                        break;
                                }
                            }
                            return nres;
                        }
                        break;
                    case e_Step_ConveryUp.二段治具阻挡:
                        {
                            var nres = SetAir2(e_output_extCard2.上层阻挡气缸3, true, e_input_extCard1.上层阻挡气缸到位3, true);
                            if (nres != 1)
                            {
                                for (int t = 0; t < GlobalVar.userConfig.airRepeatTime; t++)
                                {
                                    var res = GlobalMotion.MachineState(e_MachineStatus.暂停控制);
                                    if (res == -1)//触发了急停
                                        return -1;
                                    Thread.Sleep(GlobalVar.userConfig.airInterval);
                                    SetAir(e_output_extCard2.上层阻挡气缸3, false, e_input_extCard2.上层阻挡气缸原点3, true);
                                    nres = SetAir2(e_output_extCard2.上层阻挡气缸3, true, e_input_extCard1.上层阻挡气缸到位3, true);

                                    if (nres == 1)
                                        break;
                                }
                            }
                            return nres;
                        }
                        break;
                    case e_Step_ConveryUp.一段治具下降:
                        {
                            var nres = SetAir(e_output_extCard2.上层顶升气缸1, false, e_input_extCard2.上层顶升气缸原点1, true);
                            if (nres != 1)
                            {
                                for (int t = 0; t < GlobalVar.userConfig.airRepeatTime; t++)
                                {
                                    var res = GlobalMotion.MachineState(e_MachineStatus.暂停控制);
                                    if (res == -1)//触发了急停
                                        return -1;
                                    Thread.Sleep(GlobalVar.userConfig.airInterval);
                                    SetAir2(e_output_extCard2.上层顶升气缸1, true, e_input_extCard1.上层顶升气缸到位1, true);
                                    nres = SetAir(e_output_extCard2.上层顶升气缸1, false, e_input_extCard2.上层顶升气缸原点1, true);

                                    if (nres == 1)
                                        break;
                                }
                            }
                            if (nres != 1 && e_Staus != e_MachineStatus.无控制)
                            {
                                for (int t = 0; t < 2; t++)
                                {
                                    //GlobalVar.isPause = true;
                                    if (1 == GlobalMotion.TipInfo($"{e_Step}失败,是否重试?  是:重试   否急停"))
                                    {
                                        t = 0;
                                        SetAir2(e_output_extCard2.上层顶升气缸1, true, e_input_extCard1.上层顶升气缸到位1, true);
                                        nres = SetAir(e_output_extCard2.上层顶升气缸1, false, e_input_extCard2.上层顶升气缸原点1, true);
                                        if (nres == 1)
                                            break;
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                                GlobalVar.isPause = false;
                            }
                            return nres;
                        }
                        break;
                    case e_Step_ConveryUp.一段治具升起:
                        {
                            if (Input_CardEx2[(int)e_input_extCard2.上输送治具检测1] && Input_CardEx2[(int)e_input_extCard2.上输送治具检测2])
                            {
                                GlobalMotion.TipInfo($"入料信号检测超时，请检查是否堆料?");
                            }

                            var nres = SetAir2(e_output_extCard2.上层顶升气缸1, true, e_input_extCard1.上层顶升气缸到位1, true);
                            if (nres != 1)
                            {
                                Thread.Sleep(10);
                                SetAir2(e_output_extCard2.上层顶升气缸1, false, e_input_extCard1.上层顶升气缸到位1, true);
                                nres = SetAir2(e_output_extCard2.上层顶升气缸1, true, e_input_extCard1.上层顶升气缸到位1, true);
                                if (nres != 1)
                                {
                                    //治具不到位
                                    //GlobalVar.isPause = true;
                                    if (1 == GlobalMotion.TipInfo($"一段治具升起不到位，是否重试?"))
                                    {
                                        for (int t = 0; t < GlobalVar.userConfig.airRepeatTime; t++)
                                        {
                                            var res = GlobalMotion.MachineState(e_MachineStatus.暂停控制);
                                            if (res == -1)//触发了急停
                                                return -1;

                                            SetAir(e_output_extCard2.上层顶升气缸1, false, e_input_extCard2.上层顶升气缸原点1, true);
                                            Thread.Sleep(GlobalVar.userConfig.airInterval);
                                            nres = SetAir2(e_output_extCard2.上层顶升气缸1, true, e_input_extCard1.上层顶升气缸到位1, true);
                                            if (nres == 1)
                                                break;
                                        }
                                    }
                                    GlobalVar.isPause = false;
                                }     
                            }

                            if (nres != 1 && e_Staus != e_MachineStatus.无控制)
                            {
                                for (int t = 0; t < 1; t++)
                                {
                                    //GlobalVar.isPause = true;
                                    if (1 == GlobalMotion.TipInfo($"{e_Step}失败,是否再重试?  是:重试   否急停"))
                                    {
                                        //t = 0;
                                        SetAir(e_output_extCard2.上层顶升气缸1, false, e_input_extCard2.上层顶升气缸原点1, true);
                                        nres = SetAir2(e_output_extCard2.上层顶升气缸1, true, e_input_extCard1.上层顶升气缸到位1, true);
                                        if (nres == 1)
                                            break;
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                                GlobalVar.isPause = false;
                            }
                            return nres;
                        }
                        break;
                    case e_Step_ConveryUp.一段治具放行:
                        {
                            if (Input_CardEx2[(int)e_input_extCard2.上输送治具检测1] && Input_CardEx2[(int)e_input_extCard2.上输送治具检测2])
                            {
                                GlobalMotion.TipInfo($"入料信号检测超时，请检查是否卡盘?");
                            }

                            var nres = SetAir(e_output_extCard2.上层阻挡气缸2, false, e_input_extCard2.上层阻挡气缸原点2, true);

                            if (nres != 1)
                            {
                                for (int t = 0; t < GlobalVar.userConfig.airRepeatTime; t++)
                                {
                                    var res = GlobalMotion.MachineState(e_MachineStatus.暂停控制);
                                    if (res == -1)//触发了急停
                                        return -1;
                                    Thread.Sleep(GlobalVar.userConfig.airInterval);
                                    SetAir2(e_output_extCard2.上层阻挡气缸2, true, e_input_extCard1.上层阻挡气缸到位2, true);
                                    nres = SetAir(e_output_extCard2.上层阻挡气缸2, false, e_input_extCard2.上层阻挡气缸原点2, true);
                                    if (nres == 1)
                                        break;
                                }
                            }
                            return nres;
                        }
                        break;
                    case e_Step_ConveryUp.一段治具阻挡:
                        {
                            var nres = SetAir2(e_output_extCard2.上层阻挡气缸2, true, e_input_extCard1.上层阻挡气缸到位2, true);
                            if (nres != 1)
                            {
                                for (int t = 0; t < GlobalVar.userConfig.airRepeatTime; t++)
                                {
                                    var res = GlobalMotion.MachineState(e_MachineStatus.暂停控制);
                                    if (res == -1)//触发了急停
                                        return -1;
                                    Thread.Sleep(GlobalVar.userConfig.airInterval);
                                    SetAir(e_output_extCard2.上层阻挡气缸2, false, e_input_extCard2.上层阻挡气缸原点2, true);
                                    nres = SetAir2(e_output_extCard2.上层阻挡气缸2, true, e_input_extCard1.上层阻挡气缸到位2, true);
                                    if (nres == 1)
                                        break;
                                }
                            }
                            return nres;
                        }
                        break;
                }
                return -1;
            }
            catch (Exception ex)
            {
                displalyInfo.displayInfoMain("Step_ConveryUp" + ex.Message);
                return -1;
            }
        }

        private static Stopwatch swIn = new Stopwatch();
        private static Stopwatch swTime = new Stopwatch();
        /// <summary>
        /// 流程-前端入口工位流程
        /// </summary>
        public static void Task_ConverFront()
        {
            try
            {
                var nstop = -1;
                string logFlow = e_ConveryFront.ToString();
                if (GlobalVar.front_pros == null)
                    GlobalVar.front_pros = new List<ProductInfo>();
                bool isFirst = true;
                while (!GlobalVar.isExit)
                {
                    Thread.Sleep(8);
                    if (!GlobalVar.isEstop)
                    {
                        if (GlobalVar.isAutoRun)
                        {
                            switch (e_ConveryFront)
                            {
                                case e_Flow_ConveryUp.等待入料治具到位:
                                    if (isFirst)
                                    {
                                        var nres = Step_ConveryUp(e_Step_ConveryUp.入料段治具阻挡);
                                        if (nres != 1)
                                        {
                                            MachineState(e_MachineStatus.异常急停, $"{e_ConveryFront.ToString()}:入料段治具阻挡");
                                            continue;
                                        }
                                        swTime.Restart();
                                        isFirst = false;
                                    }

                                    if (GlobalVar.IsEndWork || GlobalVar.isPause)//结束作业就关闭要料
                                    {
                                        if (GlobalVar.userConfig.isEnableUpSignal)
                                            GlobalMotion.Card_1.setOut((ushort)e_output_card1.上层向上游要料, e_out_OnOff.on);
                                        break;
                                    }

                                    bool Ex2 = Input_CardEx2[(int)e_input_extCard2.上输送治具检测2];
                                    bool Ex3 = Input_CardEx2[(int)e_input_extCard2.上输送治具检测3];
                                    bool Px2 = Input_CardEx1[(int)e_input_extCard1.上层阻挡气缸到位2];
                                    {
                                        if (!Ex2 && !GlobalVar.isPause && !GlobalVar.isEstop && Px2)//贴标段无料  暂停/急停
                                        {
                                            if (GlobalVar.front_pros.Count > 1 && Input_CardEx2[(int)e_input_extCard2.上输送治具检测1])
                                            {

                                                //     e_ConveryFront = e_Flow_ConveryUp.入料治具到位;
                                            }
                                            else
                                            {
                                                if (!GlobalVar.userConfig.isEnableUpSignal)
                                                {
                                                    if (Input_CardEx2[(int)e_input_extCard2.上输送治具检测1])
                                                    {
                                                        GlobalVar.front_pros.Add(new ProductInfo()
                                                        {
                                                            LaserTime = DateTime.Now,
                                                            typeName = GlobalVar.closeSave.CurrentTypeName,
                                                            ProNo = 1,
                                                            Result_up = e_Result.进料OK.ToString()
                                                        });
                                                        GlobalVar.front_pros.Add(new ProductInfo()
                                                        {
                                                            LaserTime = DateTime.Now,
                                                            typeName = GlobalVar.closeSave.CurrentTypeName,
                                                            ProNo = 2,
                                                            Result_up = e_Result.进料OK.ToString()
                                                        });
                                                        e_ConveryFront = e_Flow_ConveryUp.入料治具到位;
                                                    }
                                                }
                                                else if (GlobalVar.userConfig.isEnableUpSignal)
                                                {
                                                    #region 上层要料
                                                    bool IgnoreSigalNG = GlobalMotion.Input_Card1[(int)e_input_card1.上层上游物料NG];
                                                    bool IgnoreSigalOK = GlobalMotion.Input_Card1[(int)e_input_card1.上层上游物料OK];
                                                    if (GlobalVar.userConfig.IgnoreFrontSignal)
                                                    {
                                                        IgnoreSigalNG = false;
                                                    }
                                                    if (!IgnoreSigalOK && !IgnoreSigalNG || Input_CardEx2[(int)e_input_extCard2.上输送治具检测1])
                                                    {
                                                        Step_ConveryUp(e_Step_ConveryUp.入料段治具放行);
                                                        if (!Input_CardEx2[(int)e_input_extCard2.上输送治具检测2] &&
                                                            e_ConveryLaser != e_Flow_ConveryUp.等待一段治具离开)
                                                        {
                                                            GlobalMotion.Card_1.setOut((ushort)e_output_card1.上层向上游要料, e_out_OnOff.off);
                                                        }
                                                    }
                                                       

                                                    if (!Input_CardEx2[(int)e_input_extCard2.上输送治具检测1] && !Input_CardEx2[(int)e_input_extCard2.上输送治具检测2])
                                                    {
                                                        Step_ConveryUp(e_Step_ConveryUp.入料段治具放行);
                                                        if (!Input_CardEx2[(int)e_input_extCard2.上输送治具检测2]&&
                                                            e_ConveryLaser != e_Flow_ConveryUp.等待一段治具离开)
                                                        {
                                                            GlobalMotion.Card_1.setOut((ushort)e_output_card1.上层向上游要料, e_out_OnOff.off);
                                                        }
                                                    }
                                                    #endregion

                                                    #region OK处理 等待上层信号
                                                    if (IgnoreSigalOK && Input_CardEx2[(int)e_input_extCard2.上输送治具检测1])
                                                    {
                                                        GlobalMotion.Card_1.setOut((ushort)e_output_card1.上层向上游要料, e_out_OnOff.on);
                                                        GlobalVar.front_pros.Add(new ProductInfo()
                                                        {
                                                            LaserTime = DateTime.Now,
                                                            typeName = GlobalVar.closeSave.CurrentTypeName,
                                                            ProNo = 1,
                                                            Result_up = e_Result.进料OK.ToString()
                                                        });
                                                        GlobalVar.front_pros.Add(new ProductInfo()
                                                        {
                                                            LaserTime = DateTime.Now,
                                                            typeName = GlobalVar.closeSave.CurrentTypeName,
                                                            ProNo = 2,
                                                            Result_up = e_Result.进料OK.ToString()
                                                        });
                                                        e_ConveryFront = e_Flow_ConveryUp.入料治具到位;
                                                    }
                                                    #endregion

                                                    #region NG处理
                                                    if (IgnoreSigalNG && Input_CardEx2[(int)e_input_extCard2.上输送治具检测1])
                                                    {
                                                        GlobalMotion.Card_1.setOut((ushort)e_output_card1.上层向上游要料, e_out_OnOff.on);
                                                        GlobalVar.front_pros.Add(new ProductInfo()
                                                        {
                                                            LaserTime = DateTime.Now,
                                                            typeName = GlobalVar.closeSave.CurrentTypeName,
                                                            ProNo = 1,
                                                            Result_up = e_Result.进料OK.ToString()
                                                        });
                                                        //  Result_up = e_Result.进料NG.ToString() });
                                                        GlobalVar.front_pros.Add(new ProductInfo()
                                                        {
                                                            LaserTime = DateTime.Now,
                                                            typeName = GlobalVar.closeSave.CurrentTypeName,
                                                            ProNo = 2,
                                                            //   Result_up = e_Result.进料NG.ToString() });
                                                            Result_up = e_Result.进料OK.ToString()
                                                        });
                                                        e_ConveryFront = e_Flow_ConveryUp.入料治具到位;
                                                    }
                                                    #endregion
                                                }
                                            }
                                        }
                                        else
                                        {
                                            bool checkEx1 = Input_CardEx2[(int)e_input_extCard2.上输送治具检测1];
                                            bool checkEx2 = Input_CardEx2[(int)e_input_extCard2.上输送治具检测2];
                                            if (GlobalVar.userConfig.isEnableUpSignal && Ex2)
                                            {
                                                GlobalMotion.Card_1.setOut((ushort)e_output_card1.上层向上游要料, e_out_OnOff.on);
                                            }
                                            else
                                            {
                                                if (swTime.ElapsedMilliseconds>5000 && e_ConveryLaser != e_Flow_ConveryUp.等待一段治具离开)
                                                {
                                                    swTime.Restart();
                                                    //if (Input_CardEx2[(int)e_input_extCard2.上输送治具检测1] && Ex2 && Ex3)
                                                    {
                                                        Step_ConveryUp(e_Step_ConveryUp.入料段治具放行);
                                                        Step_ConveryUp(e_Step_ConveryUp.一段治具阻挡);
                                                        if (!Input_CardEx2[(int)e_input_extCard2.上输送治具检测2])
                                                        {
                                                            GlobalMotion.Card_1.setOut((ushort)e_output_card1.上层向上游要料, e_out_OnOff.off);
                                                        }
                                                    }
                                                }
                                                else
                                                {
                                                    if (swTime.ElapsedMilliseconds > 6000)
                                                    {
                                                        swTime.Restart();
                                                    }
                                                   
                                                }
                                            }
                                        }
                                    }
                                    break;
                                case e_Flow_ConveryUp.入料治具到位:
                                    {
                                        GlobalVar.sw_CT.Restart();
                                        if (GlobalVar.userConfig.isEnableUpSignal)
                                        {
                                            GlobalMotion.Card_1.setOut((ushort)e_output_card1.上层向上游要料, e_out_OnOff.on);
                                        }
                                        var nres = Step_ConveryUp(e_Step_ConveryUp.入料段治具放行);

                                        if (!GlobalVar.IsThrought)
                                        {
                                            if (nres != 1)
                                            {
                                                MachineState(e_MachineStatus.异常急停, $"{e_ConveryFront.ToString()}:入料段治具放行");
                                                continue;
                                            }
                                        }
                                       
                                        e_ConveryFront = e_Flow_ConveryUp.入料成功;
                                        swIn.Restart();
                                    }
                                    break;
                                case e_Flow_ConveryUp.入料成功:
                                    {
                                        if (!Input_CardEx2[(int)e_input_extCard2.上输送治具检测1] && Input_CardEx2[(int)e_input_extCard2.上输送治具检测2])
                                        {
                                            isFirst = true;
                                            e_ConveryFront = e_Flow_ConveryUp.等待入料治具到位;
                                        }

                                        if (swIn.ElapsedMilliseconds > GlobalVar.userConfig.TimeOut_In)
                                        {
                                            //GlobalVar.isPause = true;
                                            if (GlobalMotion.TipInfo("入料超时  请检查是否卡盘？是：暂停处理     否：继续等待治具离开") == 1)
                                            {
                                                GlobalMotion.MachineState(e_MachineStatus.暂停控制);
                                            }
                                            GlobalVar.isPause = false;
                                            swIn.Restart();
                                        }
                                    }
                                    break;
                            }
                            if (logFlow != e_ConveryFront.ToString())
                            {
                                logFlow = e_ConveryFront.ToString();
                                displalyInfo.displayInfoMain(logFlow);
                            }
                        }
                        else
                        {
                            isFirst = true;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                displalyInfo.displayInfoMain("Task_CarrierConvery" + ex.Message);
            }
        }

        /// <summary>
        /// 流程-上层输送
        /// </summary>
        public static void Task_ConveryUp()
        {
            try
            {
                bool IsRun = false;
                bool StartUp = false;
                while (!GlobalVar.isExit)
                {

                    Thread.Sleep(8);
                    if (!GlobalVar.isEstop)
                    {
                        if (GlobalVar.isAutoRun)
                        {

                            if (GlobalMotion.e_ConveryFront == e_Flow_ConveryUp.入料成功 || GlobalMotion.e_ConveryFront == e_Flow_ConveryUp.入料治具到位 ||
                              GlobalMotion.e_ConveryLaser == e_Flow_ConveryUp.一段治具到位 || GlobalMotion.e_ConveryLaser == e_Flow_ConveryUp.等待一段治具离开 || GlobalMotion.e_ConveryLaser == e_Flow_ConveryUp.一段放行 ||
                              GlobalMotion.e_ConveryLabel == e_Flow_ConveryUp.二段治具到位 || GlobalMotion.e_ConveryLabel == e_Flow_ConveryUp.等待二段治具离开 || GlobalMotion.e_ConveryLabel == e_Flow_ConveryUp.二段写入信息与放行 || GlobalMotion.e_ConveryLabel == e_Flow_ConveryUp.上层下游交互)
                            {
                                IsRun = true;
                                IsProccessPro = true;
                            }
                            else
                            {
                                bool IgnoreSigal = GlobalMotion.Input_Card1[(int)e_input_card1.上层上游物料NG];
                                if (GlobalVar.userConfig.IgnoreFrontSignal)
                                {
                                    IgnoreSigal = false;
                                }

                                if (GlobalMotion.e_ConveryLabel == e_Flow_ConveryUp.送料中途下游停止要料)
                                {
                                    IsProccessPro = true;
                                    IsRun = false;
                                }
                                else if (GlobalVar.IsStart_EndWork)
                                {
                                    IsProccessPro = false;
                                    IsRun = false;
                                }
                                else if (Input_CardEx1[(ushort)e_input_extCard1.上层顶升气缸到位1] && Input_CardEx1[(ushort)e_input_extCard1.上层顶升气缸到位2])
                                {
                                    ///两载具
                                    IsRun = false;
                                    IsProccessPro = true;
                                }
                                else if (Input_CardEx1[(ushort)e_input_extCard1.上层顶升气缸到位1] && !Input_CardEx2[(int)e_input_extCard2.上输送治具检测3] && !Input_CardEx2[(int)e_input_extCard2.上输送治具检测4])
                                {
                                    IsRun = false;
                                    IsProccessPro = true;
                                }
                                else if (Input_CardEx1[(ushort)e_input_extCard1.上层顶升气缸到位2] && !Input_CardEx2[(int)e_input_extCard2.上输送治具检测1] && !Input_CardEx2[(int)e_input_extCard2.上输送治具检测2])
                                {
                                    ///无载具
                                    if (IgnoreSigal || Input_Card1[(int)e_input_card1.上层上游物料OK])
                                        IsRun = true;
                                    else
                                        IsRun = false;
                                    IsProccessPro = true;
                                }
                                else if (!Input_CardEx2[(int)e_input_extCard2.上输送治具检测1] && !Input_CardEx2[(int)e_input_extCard2.上输送治具检测2]
                                    && !Input_CardEx2[(int)e_input_extCard2.上输送治具检测3] && !Input_CardEx2[(int)e_input_extCard2.上输送治具检测4])
                                {
                                    IsProccessPro = false;
                                    ///无载具
                                    if (IgnoreSigal || Input_Card1[(int)e_input_card1.上层上游物料OK])
                                        IsRun = true;
                                    else
                                    {
                                        IsRun = false;
                                        System.Threading.Thread.Sleep(1500);
                                    }
                                       
                                }
                                else
                                {
                                    IsRun = true;
                                    IsProccessPro = false;
                                }
                            }

                            if (IsRun && !GlobalVar.isEstop && !GlobalVar.isExit && !GlobalVar.isPause)
                            {
                                if (!GlobalMotion.Card_1.AxisList[(int)e_card1_axis.上层输送左].m_AxisSts.Motion)
                                    GlobalMotion.ConveryControrl(e_ConveryControl.上层输送启动);
                            }
                            else
                            {
                                if (GlobalMotion.Card_1.AxisList[(int)e_card1_axis.上层输送左].m_AxisSts.Motion)
                                    GlobalMotion.ConveryControrl(e_ConveryControl.上层输送停止);
                            }
                        }
                        else
                        {
                            StartUp = false;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                displalyInfo.displayInfoMain("Task_CarrierConvery" + ex.Message);
            }
        }




    }
}
